<?php

 function connectdb() {
	$db=mysql_connect('localhost','root','') or die("bad"); 
	mysql_select_db('elibrary');
	return $db;
}

function get_spec() {
        $db = connectdb();
        $query = "SELECT * FROM spec";
        $res = mysql_query($query);
        $get_list = array();
        while($elem = mysql_fetch_array($res)) {
                $get_list[] = $elem;
        }
        return $get_list;
}

function get_subj() {
        $db = connectdb();
        $query = "SELECT * FROM subj";
        $res = mysql_query($query);
        $get_list = array();
        while($elem = mysql_fetch_array($res)) {
                $get_list[] = $elem;
        }
        return $get_list;
}


function check_user($login, $hash) {
    $db = connectdb();
    $query = "SELECT * FROM users WHERE login = '$login' AND pass = '$hash'";
    $res = mysql_query($query);
    return mysql_num_rows($res);
}

function auth() {
    session_start();
    if(!isset($_SESSION['login'])){ 
        if( isset($_COOKIE['login']) && isset($_COOKIE['pass'])) {
            $login = $_COOKIE['login'];
            $pass_hash = $_COOKIE['pass'];
            if(check_user($login, $pass_hash)) {
                $_SESSION['login'] = $login;
            }
        }
    } 
	else {
		$login = $_SESSION['login'];
    }
    return $login;
}

?>